//
//  KLMTimePickerViewController.swift
//  KLM_GN
//
//  Created by Nic on 2025/3/27.
//

import UIKit

<<<<<<< HEAD
class KLMTimePickerViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
    
    // MARK: - UI Components
    private let timePicker = UIPickerView()
    private let buttonStack = UIStackView()
    
    // MARK: - Data
    var minHour = 0
    var maxHour = 23
    var selectedHour = 0
    var selectedMinute = 0
    
    var selectIndex: (Int, Int) = (0, 0)
    
    
    var onTimeSelected: ((Int, Int) -> Void)?
    
    // MARK: - Lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        setupConstraints()
        setupInitialSelection()
    }
    
    // MARK: - UI Setup
    private func setupUI() {
        view.backgroundColor = .white
        
        // 配置时间选择器
        timePicker.dataSource = self
        timePicker.delegate = self
        view.addSubview(timePicker)
        
        // 配置按钮
        let cancelButton = UIButton(type: .custom)
        cancelButton.setTitle("取消", for: .normal)
        cancelButton.setTitleColor(rgba(38, 38, 38, 1), for: .normal)
        cancelButton.addTarget(self, action: #selector(handleCancel), for: .touchUpInside)
        
        let confirmButton = UIButton(type: .custom)
        confirmButton.setTitle("确定", for: .normal)
        confirmButton.setTitleColor(appMainThemeColor, for: .normal)
        confirmButton.addTarget(self, action: #selector(handleConfirm), for: .touchUpInside)
        
        buttonStack.axis = .horizontal
        buttonStack.distribution = .fillEqually
        buttonStack.spacing = 20
        buttonStack.addArrangedSubview(cancelButton)
        buttonStack.addArrangedSubview(confirmButton)
        view.addSubview(buttonStack)
        buttonStack.backgroundColor = .red
    }
    
    // MARK: - Picker Configuration
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 2 // 小时和分钟
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if component == 0 { // 小时
            return maxHour - minHour + 1
        } else { // 分钟
            return 60
        }
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if component == 0 {
            return String(format: "%02d时", minHour + row)
        } else {
            return String(format: "%02d分", row)
        }
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if component == 0 {
            selectedHour = minHour + row
        } else {
            selectedMinute = row
        }
    }
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 36
    }
    
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        let contentView = UIView()
        let topLine = UIView()
        let bottomLine = UIView()
        
        return contentView
    }
    // MARK: - Button Actions
    @objc private func handleCancel() {
        dismiss(animated: true)
    }
    
    @objc private func handleConfirm() {
        onTimeSelected?(selectedHour, selectedMinute)
        dismiss(animated: true)
    }
    
    // MARK: - Initial Setup
    private func setupInitialSelection() {
        let initialHour = selectedHour.clamped(to: minHour...maxHour)
        let initialMinute = selectedMinute.clamped(to: 0...59)
        
        timePicker.selectRow(initialHour - minHour, inComponent: 0, animated: false)
        timePicker.selectRow(initialMinute, inComponent: 1, animated: false)
        
        selectIndex = (initialHour - minHour, initialMinute)
    }
    
    // MARK: - Layout
    private func setupConstraints() {
        timePicker.translatesAutoresizingMaskIntoConstraints = false
        buttonStack.translatesAutoresizingMaskIntoConstraints = false
        
        NSLayoutConstraint.activate([
            timePicker.topAnchor.constraint(equalTo: view.topAnchor, constant: 0),
            timePicker.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0),
            timePicker.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0),
            timePicker.bottomAnchor.constraint(equalTo: buttonStack.topAnchor),
            
            buttonStack.heightAnchor.constraint(equalToConstant: 44.0),
            buttonStack.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0),
            buttonStack.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0),
            buttonStack.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0)
        ])
        
        preferredContentSize = CGSize(width: 295, height: 222)
    }
}

// MARK: - Popover Presentation
extension UIViewController {
    func showTimePicker(
        sourceView: UIView,
        initialHour: Int = 0,
        initialMinute: Int = 0,
        minHour: Int = 0,
        maxHour: Int = 23,
        completion: @escaping (Int, Int) -> Void
    ) {
        let pickerVC = KLMTimePickerViewController()
        pickerVC.modalPresentationStyle = .popover
        pickerVC.selectedHour = initialHour
        pickerVC.selectedMinute = initialMinute
        pickerVC.minHour = minHour
        pickerVC.maxHour = maxHour
        pickerVC.onTimeSelected = completion
        
        let popover = pickerVC.popoverPresentationController
        popover?.sourceView = sourceView
        popover?.sourceRect = sourceView.bounds
        popover?.permittedArrowDirections = .up
        
        present(pickerVC, animated: true)
    }
}
//
//// 扩展：数值范围限制
extension Comparable {
    func clamped(to limits: ClosedRange<Self>) -> Self {
        return min(max(self, limits.lowerBound), limits.upperBound)
    }
}
//
//// 使用示例
//class ViewController: UIViewController {
//    @IBAction func showTimePicker(_ sender: UIButton) {
//        showTimePicker(sourceView: sender, initialHour: 19, initialMinute: 30) { hour, minute in
//            print("选择时间：\(String(format: "%02d:%02d", hour, minute))")
//        }
//    }
//}
=======
class KLMTimePickerViewController: KLMBaseController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
>>>>>>> c9ff26f7439158038ece58549346ff469099cbd4
